package org.example.coknown.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.coknown.entity.TraceabilityInformation;
import org.example.coknown.mapper.TraceabilityInformationMapper;
import org.example.coknown.service.TraceabilityInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TraceabilityInformationServiceImpl implements TraceabilityInformationService{
    @Autowired
    private TraceabilityInformationMapper traceabilityInformationMapper;

    @Override
    public IPage<TraceabilityInformation> listUsers(Integer page, Integer pageSize) {
        // 创建分页对象
        Page<TraceabilityInformation> traceabilityPage = new Page<>(page, pageSize);
        // 调用 Mapper 接口的分页查询方法
        return traceabilityInformationMapper.selectPage(traceabilityPage, null);
    }

    /**
     * 根据id查询溯源信息
     * @param id
     * @return
     */
    public List<TraceabilityInformation> selectById(Integer id){

        return traceabilityInformationMapper.selectById(id);
    }

    /**
     * 添加溯源信息
     *
     * @param traceabilityInformation 返回1表示添加成功 0表示添加失败
     */
    public int addTraceabilityInformation(TraceabilityInformation traceabilityInformation) {
        // 设置更新时间
        traceabilityInformation.setUpdateTime(LocalDateTime.now());
        traceabilityInformationMapper.insert(traceabilityInformation);
        return 1;
    }

    /**
     * 根据betch查询溯源信息，成功返回查询信息，失败返回null
     * @param betch
     * @return
     */
    public List<TraceabilityInformation> selectByBetch(Integer betch) {
        return traceabilityInformationMapper.selectByBetch(betch);
    }

}

